package com.yhn.init.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.constant.FileConstant;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.mapper.FinancialDetailsMapper;
import com.yhn.init.model.dto.financialDetails.FinancialDetailsAddRequest;
import com.yhn.init.model.dto.financialDetails.FinancialDetailsQueryRequest;
import com.yhn.init.model.dto.financialDetails.FinancialDetailsUpdateRequest;
import com.yhn.init.model.entity.FinancialDetails;
import com.yhn.init.model.vo.FinancialDetailsVO;
import com.yhn.init.service.FileService;
import com.yhn.init.service.FinancialDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yhnyhn123
 */

@Slf4j
@Service("financialDetailsService")
public class FinancialDetailsServiceImpl extends ServiceImpl<FinancialDetailsMapper, FinancialDetails> implements FinancialDetailsService {

    @Resource
    private FileService fileService;

    @Override
    public Page<FinancialDetailsVO> queryPage(FinancialDetailsQueryRequest financialDetailsQueryRequest) {
        if (financialDetailsQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = financialDetailsQueryRequest.getCurrent();
        long size = financialDetailsQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<FinancialDetails> financialDetailsPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(financialDetailsQueryRequest));
        Page<FinancialDetailsVO> financialDetailsVOPage = new Page<>(current, size, financialDetailsPage.getTotal());
        List<FinancialDetailsVO> financialDetailsVO = this.getFinancialDetailsVO(financialDetailsPage.getRecords());
        financialDetailsVOPage.setRecords(financialDetailsVO);
        return financialDetailsVOPage;
    }


    @Override
    public QueryWrapper<FinancialDetails> getQueryWrapper(FinancialDetailsQueryRequest financialDetailsQueryRequest) {
        QueryWrapper<FinancialDetails> queryWrapper = new QueryWrapper<>();
        // 如果请求对象为空，直接返回一个空的查询包装器
        if (financialDetailsQueryRequest == null) {
            return queryWrapper;
        }
        // 从请求对象中获取各项查询条件
        Long id = financialDetailsQueryRequest.getId();
        queryWrapper.eq(id != null && id > 0, "id", id);
        return queryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param financialDetailsList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<FinancialDetailsVO> getFinancialDetailsVO(List<FinancialDetails> financialDetailsList) {
        if (CollUtil.isEmpty(financialDetailsList)) {
            return new ArrayList<>();
        }
        return financialDetailsList.stream().map(this::getFinancialDetailsVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param financialDetails 原对象
     * @return 脱敏对象
     */
    @Override
    public FinancialDetailsVO getFinancialDetailsVO(FinancialDetails financialDetails) {
        if (financialDetails == null) {
            return null;
        }
        FinancialDetailsVO financialDetailsVO = new FinancialDetailsVO();
        BeanUtils.copyProperties(financialDetails, financialDetailsVO);
        return financialDetailsVO;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public FinancialDetailsVO getInfoById(Long id) {
        FinancialDetails financialDetails = this.getById(id);
        if (financialDetails == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getFinancialDetailsVO(financialDetails);
    }

    /**
     * 保存信息
     *
     * @param financialDetailsAddRequest 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public FinancialDetailsVO saveFinancialDetails(FinancialDetailsAddRequest financialDetailsAddRequest, MultipartFile receiptPhoto) {
        if (financialDetailsAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String uploadedImage = fileService.uploadImage(receiptPhoto, FileConstant.FILE_UPLOAD_PATH_IMG);
        FinancialDetails financialDetails = new FinancialDetails();
        BeanUtils.copyProperties(financialDetailsAddRequest, financialDetails);
        financialDetails.setReceiptPhoto(uploadedImage);
        boolean result = this.save(financialDetails);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return getFinancialDetailsVO(financialDetails);

    }


    /**
     * 修改信息
     *
     * @param financialDetailsUpdateRequest 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateFinancialDetails(FinancialDetailsUpdateRequest financialDetailsUpdateRequest) {
        if (financialDetailsUpdateRequest == null || financialDetailsUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        FinancialDetails financialDetails = new FinancialDetails();
        BeanUtils.copyProperties(financialDetailsUpdateRequest, financialDetails);
        long id = financialDetailsUpdateRequest.getId();
        // 判断是否存在
        FinancialDetails oldFinancialDetails = this.getById(id);
        ThrowUtils.throwIf(oldFinancialDetails == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(financialDetails);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }
}
